﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Desktop.Upgrade.DataAccess
{
    class LocalAccess
    {
        public bool UpdateFileInfo(string fileName, string fileMd5)
        {
            SQLiteConnection conn = new SQLiteConnection("data source=data.db3");
            SQLiteCommand comm = null;

            try
            {
                conn.Open();

                string sql = $"update upgrade_info set file_md5 = '{fileMd5}' where file_name='{fileName}'";
                comm = new SQLiteCommand(sql, conn);
                int count = comm.ExecuteNonQuery();
                if (count == 0)
                {
                    // 如果没有修改，只能做新增
                    comm.CommandText = $"insert into upgrade_info(file_name,file_md5) values('{fileName}','{fileMd5}')";
                    count = comm.ExecuteNonQuery();
                    if (count == 0)
                        return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (comm != null)
                {
                    comm.Dispose();
                    comm = null;
                }
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                    conn = null;
                }
            }
        }
    }
}
